<?php
namespace app\admin\controller;
use app\admin\logic\MatterLogic;

/**
 * 事项api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/19
 * Time: 16:00
 */
class Matter extends Base {

    public function __construct(){
        parent::__construct();
        $this->m_logic = new MatterLogic();
    }

    /**
     * 获取事项
     * @return \think\response\Json
     */
    public function ajax_get_matter(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取订单
        $r = $this->m_logic->get_matter($params);

        $data['list'] = $r['data']['list'];
        $data['total_num'] = $r['data']['total_num'];
        return json($this->m_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取事项详情
     * @return \think\response\Json
     */
    public function ajax_get_matter_detail(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['id','=',$params['matterid']];
        $m_detail = model('matter')->get_detail($maps,'id,from_openid,sourceid,source,name,out_time,c_time,deal_openid');

        //获取事项信息
        //查找发起人
        $maps = [];
        $maps[] = ['openid','=',$m_detail['from_openid']];
        $from_u_detail = model('user')->get_detail($maps,'id,username');

        //查找处理人
        $maps = [];
        $maps[] = ['openid','=',$m_detail['deal_openid']];
        $deal_u_detail = model('user')->get_detail($maps,'id,username');

        $matter_info = [];
        $matter_info['from_username'] = $from_u_detail['username'];
        $matter_info['deal_username'] = $deal_u_detail['username'];
        $matter_info['source'] = $m_detail['source'];
        $matter_info['source_zw'] = $m_detail->source_name;
        $matter_info['out_time'] = $m_detail['out_time'];
        $matter_info['c_time'] = $m_detail['c_time'];

        //获取来源信息
        $s = new \app\common\controller\Source();
        $r = $s->get_source($m_detail['sourceid'],$m_detail['source']);
        if(empty($r)){
            return json($this->m_logic->get_response(0,'该事项不存在'));
        }
        $source_info = $r['source_info'];
        $source_info['name'] = $m_detail['name'];

        //获取事项处理动态
        $m = new \app\common\controller\Matter();
        $deal = $m->get_matter_deal($m_detail['id']);
        $deal_info = $deal['deal_info'];

        $data['matter_info'] = $matter_info;
        $data['source_info'] = $source_info;
        $data['deal_info'] = $deal_info;
        return json($this->m_logic->get_response(1,'OK',$data));
    }

    /**
     * 导出事项
     * http://127.0.0.1/mandiao-mini-api/public/admin-matter-export_matter?token=e916a6039bff505bacaf5def2bb8435f&page=1&rows=10&from_username=&keywords=&status=-1&source=-1&is_out_time=-1&start_time=&end_time=
     * @return \think\response\Json
     */
    public function export_matter(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //获取事项
        $r = $this->m_logic->get_matter($params);
        $list = $r['data']['list'];

        $row = ['发起人','时间','名称','来源','事项内容','当前处理人','当前处理人是否超时','状态'];

        $export_data = [];
        foreach($list as $k => $v){
            $export_data[$k]['from_username'] = $v['from_username'];
            $export_data[$k]['out_time'] = $v['out_time'];
            $export_data[$k]['name'] = $v['name'];
            $export_data[$k]['source_zw'] = $v['source_zw'];
            $export_data[$k]['matter_content'] = ($v['source'] == 1) ? $v['source_info']['source_describe'] : $v['source_info']['source_content'];
            $export_data[$k]['deal_username'] = $v['deal_username'];
            $export_data[$k]['out_time_zw'] = $v['out_time_zw'];
            $export_data[$k]['status_zw'] = $v['status_zw'];
        }
        array_unshift($export_data,$row);

        $file_name = '事项' . date('YmdHi') . '.xlsx';
        $r = $this->m_logic->gen_matter_excel($file_name,$export_data);

        //$this->redirect(\think\facade\Request::root(true) . '/' . $file_name);
        return json($this->m_logic->get_response(1,'导出成功'));
    }
}